/*
 * @Description:
 * @Author: qingyang
 * @Date: 2022-01-17 14:10:53
 * @LastEditors: qingyang
 * @LastEditTime: 2023-07-21 10:53:57
 */
import { ElMessage, MessageParams } from 'element-plus';
import { ElMessageBox } from 'element-plus';
import type { MessageBoxState } from 'element-plus/lib/components/message-box/src/message-box.type';
import type { VNode } from 'vue';
export interface ModalOptionsEx extends Omit<MessageBoxState, 'iconType'> {
  iconType?: 'warning' | 'success' | 'error' | 'info';
}
export declare type IconType = 'success' | 'info' | 'error' | 'warning';

let msgInstance: any = null;
const createMessage: any = (options: MessageParams) => {
  if (msgInstance) {
    msgInstance.close();
  }
  msgInstance = ElMessage(options);
};
const typeArr: IconType[] = ['success', 'warning', 'error', 'info'];
typeArr.forEach(type => {
  (createMessage as any)[type] = (options: string | MessageParams) => {
    if (typeof options === 'string') {
      options = {
        message: options
      };
      options.type = type;
      return createMessage(options);
    }
  };
});
/**
 * @description: Create confirmation box
 */
function getIcon(iconType: IconType) {
  if (iconType === 'warning') {
    return <span>icon</span>;
  } else if (iconType === 'success') {
    // return <CheckCircleFilled class="modal-icon-success" />;
  } else if (iconType === 'info') {
    // return <InfoCircleFilled class="modal-icon-info" />;
  } else {
    // return <CloseCircleFilled class="modal-icon-error" />;
  }
}
function getTitle(type: IconType, title: string) {
  return `<div>${getIcon(type)}<span>${title}131</span></div>`;
}
function createConfirm(title: string, content: VNode | string, options: any) {
  const iconType = options.iconType;
  Reflect.deleteProperty(options, 'iconType');
  if (!options.cancelButtonText) {
    options.cancelButtonText = '取消';
  }
  if (!options.confirmButtonText) {
    options.confirmButtonText = '确定';
  }
  if (iconType && iconType == 'warning' && options?.btnType !== 'primary') {
    // 图标如果是警告，并且按钮不是primary， 按钮就变成警告色
    options.confirmButtonClass = 'warning-btn';
  }
  const keys = Object.keys(options);
  const opt: any = {
    type: iconType,
    centered: true,
    duration: 0,

    // icon: getIcon(iconType),
    ...options,
    customClass: iconType ? 'modal-confirm' : 'modal-without-icon',
    dangerouslyUseHTMLString: true,
    'close-on-click-modal': keys.includes('closeOnPressEscape') ? options.closeOnPressEscape : true,
    'close-on-press-escape': keys.includes('closeOnPressEscape') ? options.closeOnPressEscape : true
  };
  return ElMessageBox.confirm(content, title, opt);
}

/**
 * @description: message
 */
export function useMessage() {
  return {
    createMessage,
    createConfirm
  };
}
